package com.gw.yunke.service.impl;

import com.github.pagehelper.PageHelper;
import com.gw.yunke.dao.CommentMapper;
import com.gw.yunke.dao.PraiseUserCommentMapper;
import com.gw.yunke.dao.RecommentMapper;
import com.gw.yunke.domain.Comment;
import com.gw.yunke.domain.CommentExample;
import com.gw.yunke.domain.PraiseUserComment;
import com.gw.yunke.domain.PraiseUserCommentExample;
import com.gw.yunke.domain.Recomment;
import com.gw.yunke.domain.RecommentExample;
import com.gw.yunke.domain.dto.CommentResp;
import com.gw.yunke.domain.dto.PraiseCommentReq;
import com.gw.yunke.service.ICommentService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentServiceImpl implements ICommentService {

  public static final Logger log = LoggerFactory.getLogger(CommentServiceImpl.class);
  @Autowired
  private CommentMapper commentMapper;

  @Autowired
  private RecommentMapper recommentMapper;

  @Autowired
  private PraiseUserCommentMapper praiseUserCommentMapper;

  @CacheEvict(value = "comments", key = "#comment.videoId")
  @Override
  public int createComment(Comment comment) {
    return commentMapper.insertSelective(comment);
  }

  //@Cacheable(value = "comments")
  @Override
  public List<CommentResp> getCommentsByVideoId(Long videoId, Long userId) {
    List<CommentResp> commentRespList = new ArrayList<>();

    CommentExample example = new CommentExample();
    CommentExample.Criteria criteria = example.createCriteria();
    criteria.andVideoIdEqualTo(videoId);
    example.setOrderByClause("create_time desc");
    List<Comment> commentList = commentMapper.selectByExample(example);
    for (Comment comment : commentList) {
      CommentResp commentResp = new CommentResp();
      BeanUtils.copyProperties(comment,commentResp);
      if(userId != null) {
        PraiseUserCommentExample praiseUserCommentExample = new PraiseUserCommentExample();
        PraiseUserCommentExample.Criteria pC = praiseUserCommentExample.createCriteria();
        pC.andCommentIdEqualTo(comment.getId());
        pC.andUserIdEqualTo(userId);
        List<PraiseUserComment> praiseUserCommentList = praiseUserCommentMapper.selectByExample(praiseUserCommentExample);
        if (praiseUserCommentList.size() > 0) {
          commentResp.setHasPraise((byte) 1);
        }
      }
      log.info("[获取评论用户信息] " + comment.getUserName());
      if (comment.getType()) {
        log.info("获取问答回复信息");
        RecommentExample recommentExample = new RecommentExample();
        RecommentExample.Criteria reCriteria = recommentExample.createCriteria();
        reCriteria.andCommentIdEqualTo(comment.getId());
        List<Recomment> recommentList = recommentMapper.selectByExample(recommentExample);
        commentResp.setRecommentList(recommentList);
      }
      commentRespList.add(commentResp);
    }

    return commentRespList;
  }

  @Override
  public int createRecomment(Recomment recomment) {
    return recommentMapper.insertSelective(recomment);
  }

  @Override
  public int addCommentPraise(PraiseCommentReq praiseCommentReq) {
    PraiseUserComment praiseUserComment = new PraiseUserComment();
    praiseUserComment.setCommentId(praiseCommentReq.getCommentId());
    praiseUserComment.setUserId(praiseCommentReq.getUserId());
    try {
      Comment comment = commentMapper.selectByPrimaryKey(praiseCommentReq.getCommentId());
      praiseUserComment.setVideoId(comment.getVideoId());
      praiseUserCommentMapper.insert(praiseUserComment);
    }catch (Exception e){
      log.error("添加用户点赞时缺失video信息",e);
    }
    return commentMapper.addPraise(praiseCommentReq.getCommentId());
  }

  @Override
  public Comment getCommentById(Long commentId) {
    return commentMapper.selectByPrimaryKey(commentId);
  }

  @Override
  public List<Long> getUserPraiseInfo(Long videoId, Long userId) {
    PraiseUserCommentExample example = new PraiseUserCommentExample();
    PraiseUserCommentExample.Criteria criteria = example.createCriteria();

    List<PraiseUserComment> praiseUserCommentList = praiseUserCommentMapper.selectByExample(example);
    List<Long> commentIds = new ArrayList<>();
    for (PraiseUserComment praiseUserComment : praiseUserCommentList) {
      commentIds.add(praiseUserComment.getCommentId());
    }
    return commentIds;

  }

  @Override
  public List<CommentResp> getHotCommentsByVideoId(Long videoId,Long userId) {
    List<CommentResp> commentRespList = new ArrayList<>();
    PageHelper.startPage(1, 5);
    CommentExample example = new CommentExample();
    CommentExample.Criteria criteria = example.createCriteria();
    criteria.andVideoIdEqualTo(videoId);
    criteria.andPraiseGreaterThan(5);
    example.setOrderByClause("praise desc");
    List<Comment> commentList = commentMapper.selectByExample(example);
    for (Comment comment : commentList) {
      CommentResp commentResp = new CommentResp();
      BeanUtils.copyProperties(comment,commentResp);//commentResp.setComment(comment);
      if(userId != null) {
        PraiseUserCommentExample praiseUserCommentExample = new PraiseUserCommentExample();
        PraiseUserCommentExample.Criteria pC = praiseUserCommentExample.createCriteria();
        pC.andCommentIdEqualTo(comment.getId());
        pC.andUserIdEqualTo(userId);
        List<PraiseUserComment> praiseUserCommentList = praiseUserCommentMapper.selectByExample(praiseUserCommentExample);
        if (praiseUserCommentList.size() > 0) {
          commentResp.setHasPraise((byte) 1);
        }
      }
      log.info("[获取评论用户信息] " + comment.getUserName());
      if (comment.getType()) {
        log.info("获取问答回复信息");
        RecommentExample recommentExample = new RecommentExample();
        RecommentExample.Criteria reCriteria = recommentExample.createCriteria();
        reCriteria.andCommentIdEqualTo(comment.getId());
        List<Recomment> recommentList = recommentMapper.selectByExample(recommentExample);
        commentResp.setRecommentList(recommentList);
      }
      commentRespList.add(commentResp);
    }

    return commentRespList;
  }

  @Override
  public List<PraiseUserComment> getCommentPraiseInfo(Long commentId, Long userId) {
    PraiseUserCommentExample example = new PraiseUserCommentExample();
    PraiseUserCommentExample.Criteria criteria = example.createCriteria();
    criteria.andCommentIdEqualTo(commentId);
    criteria.andUserIdEqualTo(userId);
    List<PraiseUserComment> praiseUserCommentList = praiseUserCommentMapper.selectByExample(example);
    return praiseUserCommentList;
  }
}
